package com.topcoder.srm510;

public class TheLuckyGameDivTwo {
	public int find(int a, int b, int jLen, int bLen) {
		int[] lucky = new int[4748];
		for (int i=a; i<=b; i++) {
			int t = i;
			while (t > 0) {
				int n = t % 10;
				if (n!=4 && n!=7) {
					break;
				}
				t /= 10;
			}
			lucky[i] = lucky[i-1] + (t==0? 1 : 0);
		}
		
		int max = 0;
		for (int i = a; i <= b-jLen+1; i++) {
			int min = 4747;
		    for (int j=i; j<=i+jLen-bLen; j++) {
		    	min = Math.min(min, lucky[j+bLen-1] - lucky[j-1]);
		    }
			
			max = Math.max(max, min);
		}
		
		return max;
	}
}
